package com.yzx.lifeassistants.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yzx.lifeassistants.R;
import com.yzx.lifeassistants.bean.PartTimeInfo;

/**
 * @Description: 活动预告列表适配器
 * @author: yzx
 * @time: 2016-3-11 下午8:02:51
 */
public class PartTimeAdapter extends BaseAdapter {

	private Context context;
	private List<PartTimeInfo> dataList;
	private PartTimeInfo data;
	private LayoutInflater inflater;
	private ViewHolder holder;

	public PartTimeAdapter(Context context, List<PartTimeInfo> dataList) {
		this.context = context;
		this.dataList = dataList;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return dataList.size();
	}

	@Override
	public Object getItem(int position) {
		return dataList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = inflater.inflate(R.layout.item_part_time_list,
					parent, false);
			holder = new ViewHolder();
			holder.verifyIV = (ImageView) convertView
					.findViewById(R.id.item_part_time_iv);
			holder.dateTV = (TextView) convertView
					.findViewById(R.id.item_part_time_date_tv);
			holder.company = (TextView) convertView
					.findViewById(R.id.item_part_time_company_tv);
			holder.titleTV = (TextView) convertView
					.findViewById(R.id.item_part_time_title_tv);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		data = dataList.get(position);
		holder.dateTV.setText("发布日期：" + data.getTime());
		String title = data.getTitle();
		holder.titleTV.setText(title);
		String company = data.getCompany();
		if (7 > company.length()) {
			holder.company.setText("公司名称：" + data.getCompany());
		} else {
			holder.company.setText(data.getCompany());
		}
		if (data.getIsVerify()) {
			Drawable drawable = context.getResources().getDrawable(
					R.drawable.icon_company_verify);
			// / 这一步必须要做,否则不会显示.
			drawable.setBounds(0, 0, drawable.getMinimumWidth(),
					drawable.getMinimumHeight());
			holder.company.setCompoundDrawables(drawable, null, null, null);
			holder.verifyIV
					.setBackgroundResource(R.drawable.icon_company_verify_yes);
		} else {
			holder.company.setCompoundDrawables(null, null, null, null);
			holder.verifyIV
					.setBackgroundResource(R.drawable.icon_company_verify_not);
		}
		return convertView;
	}

	static class ViewHolder {
		ImageView verifyIV;// 验证
		TextView titleTV;// 标题
		TextView company;// 公司
		TextView dateTV;// 时间
	}
}
